home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Recursive function -> how do you exit one?
- Date: 29 Jan 1996 12:42:08 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4ejbf0INN2b3@keats.ugrad.cs.ubc.ca>
- References: <4eh1g8$aba@pulp.ucs.ualberta.ca>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4eh1g8$aba@pulp.ucs.ualberta.ca>,
- Jacob Bukczynski <jbukczyn@gpu.srv.ualberta.ca> wrote:
- >I'm using a recursive function to search for files. I need
- >it to quit when a user presses Esc.
- >
- >Using
- >
- >return;
- >
- >doesn't seem to work, the funtion runs again. ( I tested
- >this by putting a printf statement in front of the return -
- >it got printed over and over again. )
- >
- >Is there a special way of exiting a recursive function?
-
- The Elegant Way:
- ----------------
-
- Make whether or not the user wants to exit a condition of the recursion.
- Then do something like "return USERQUIT; " if the user wants to quit.
- The next higher level of recursion will detect this return value, and chain it
- by also doing "return USERQUIT; " all the way to the top level. Whoever called
- the recursive function will know that it had been aborted.
-
-
- The Real Way:
- -------------
-
- Use setjmp() to mark the stack frame before entering the recursive search.
- When the user quits, use longjmp() to throw away all the stack frames in one
- fell swoop and return to the caller. You have to be careful, since this can
- clobber the caller's local variables, which are typically assigned to
- registers. Declare the caller's locals as "volatile".
-
- >Please reply at jbukczyn@gpu.srv.ualberta.ca
-
- CC sent.
- --
-
-